package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.common.RedisConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.User;
import com.itheima.health.util.ValidateCodeUtils;
import io.swagger.annotations.Api;
import com.sun.xml.internal.bind.v2.TODO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * 验证码Controller
 */
@RestController
@RequestMapping("/mobile/validateCode")
@Slf4j
@Api(tags = "手机端验证码接口")
public class MobileValidateCodeController {

    @Autowired
    private RedisTemplate redisTemplate;

    // TODO type以后问老师

    /**
     * 生成验证码
     * @param type
     * @param telephone 用户手机号
     * @return
     */
    @PostMapping("/send")
    public Result smsLogin(Object type, String telephone) {
log.info("telephone"+telephone);
log.info("MOBILE_SIGNIN"+type);
        System.out.println(type.toString());
        // 生成验证码
        String code = ValidateCodeUtils.generateValidateCode(4).toString();

        // 先打印代替发送短息  TODO 发送短信
        System.out.println("生成的验证码是:"+code);

        redisTemplate.opsForValue().set(telephone, code, 5, TimeUnit.MINUTES);

        return new Result(true, MessageConst.SEND_VALIDATECODE_SUCCESS);
    }


}
